package com.betty.cscams.controller;

import java.util.List;

import com.betty.cscams.constant.SysConstants;
import com.betty.cscams.entity.BusIntellectual;
import com.betty.cscams.entity.BusTerm;
import com.betty.cscams.service.BusTermService;
import com.betty.cscams.service.SysFileService;
import com.betty.cscams.vo.ScoreQueryBean;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.betty.cscams.common.http.HttpResult;
import com.betty.cscams.common.page.PageRequest;

import com.betty.cscams.entity.BusMoral;
import com.betty.cscams.service.BusMoralService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * ---------------------------
 *  (BusMoralController)         
 * ---------------------------
 * 时间：  2022-03-14 16:41:44
 * ---------------------------
 */
@RestController
@RequestMapping("busMoral")
public class BusMoralController {

	@Autowired
	private BusMoralService busMoralService;
	@Autowired
	private BusTermService busTermService;
	@Autowired
	private SysFileService fileService;

	/**
	 * 保存
	 * @param record
	 * @return
	 */	
	@PostMapping(value="/save")
	public HttpResult save(@RequestBody BusMoral record) {
		BusTerm busTerm = busTermService.findById(record.getTermId());
		if(ObjectUtils.isEmpty(busTerm) || !SysConstants.TERM_STATUS_2.equals(busTerm.getStatus())){
			return HttpResult.error("当前学期未开始或已结束");
		}
		if(record.getStatus() != null && record.getStatus() !=1 && record.getStatus() !=2) {
			return HttpResult.error("该成绩已提交审核，不可修改");
		}
		return HttpResult.ok(busMoralService.save(record));
	}

    /**
     * 删除
     * @param records
     * @return
     */
	@PostMapping(value="/delete")
	public HttpResult delete(@RequestBody List<BusMoral> records) {
		for (BusMoral busTerm: records) {
			BusMoral term = busMoralService.findById(busTerm.getId());
			if(term.getStatus() != 1 && term.getStatus() != 2){
				return HttpResult.error("请选择未提交或已退回的成绩进行删除");
			}
		}
		return HttpResult.ok(busMoralService.delete(records));
	}

    /**
     * 基础分页查询
     * @param pageRequest
     * @return
     */    
	@PostMapping(value="/findPage")
	public HttpResult findPage(@RequestBody PageRequest pageRequest) {
		return HttpResult.ok(busMoralService.findPage(pageRequest));
	}
	
    /**
     * 根据主键查询
     * @param id
     * @return
     */ 	
	@GetMapping(value="/findById")
	public HttpResult findById(@RequestParam Long id) {
		return HttpResult.ok(busMoralService.findById(id));
	}

	/**
	 * 导入
	 */

	@PostMapping(value = "/importSports")
	public HttpResult importSports(MultipartFile file, Long termId) throws Exception {
		BusTerm busTerm = busTermService.findById(termId);
		if(ObjectUtils.isEmpty(busTerm) || !SysConstants.TERM_STATUS_2.equals(busTerm.getStatus())){
			return HttpResult.error("当前学期未开始");
		}else{
			fileService.importExecl(file,termId,1);
		}
		return HttpResult.ok("导入成功");
	}

	/**
	 * 导出
	 * @param params
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/exportMoral")
	public HttpResult exportMoral(@RequestBody ScoreQueryBean params, HttpServletResponse response) throws Exception {
		try{
			busMoralService.exportMoral(params,response);
			return HttpResult.ok("导出成功");
		}catch (Exception e){
			return HttpResult.error("操作失败");
		}

	}

	/**
	 * 计算
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/calculationSports")
	public HttpResult calculationSports(@RequestBody ScoreQueryBean params) throws Exception {
		BusTerm busTerm = busTermService.findById(params.getTermId());
		if(ObjectUtils.isEmpty(busTerm) || !SysConstants.TERM_STATUS_2.equals(busTerm.getStatus())){
			return HttpResult.error("当前学期未开始");
		}
		try{
			busMoralService.calculationSports(params.getTermId(),params.getDeptId());
			return HttpResult.ok("操作成功");
		}catch (Exception e){
			return HttpResult.error("操作失败");
		}

	}

	/**
	 * 清空
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/resetSports")
	public HttpResult resetSports(@RequestBody ScoreQueryBean params) throws Exception {
		BusTerm busTerm = busTermService.findById(params.getTermId());
		if(ObjectUtils.isEmpty(busTerm) || !SysConstants.TERM_STATUS_2.equals(busTerm.getStatus())){
			return HttpResult.error("当前学期未开始");
		}
		try{
			busMoralService.calculationReset(params.getTermId() ,params.getDeptId());
			return HttpResult.ok("操作成功");
		}catch (Exception e){
			return HttpResult.error("操作失败");
		}

	}

	/**
	 * 基础分页待办
	 * @param pageRequest
	 * @return
	 */
	@PostMapping(value="/findPageTo")
	public HttpResult findPageTo(@RequestBody PageRequest pageRequest) {
		return HttpResult.ok(busMoralService.findPageTo(pageRequest));
	}
}
